package com.cqrt.aop;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.cqrt.mapper.RTTaskMapper;
import com.cqrt.mapper.RtWorkItemMapper;
import com.github.pagehelper.PageInfo;
import com.hustcad.plm.pdm.core.response.TyppmResponseResult;
import com.hustcad.plm.rpm.entityconvert.BaseConvert;
import com.hustcad.plm.rpm.model.dto.job.BatchJobDTO;
import com.hustcad.plm.rpm.model.dto.job.JobStatusUpdateDTO;
import com.hustcad.plm.rpm.model.dto.job.TyppmJobDTO;
import com.hustcad.plm.rpm.model.entity.outrule.TyppmOutRuleItem;
import com.hustcad.plm.rpm.model.vo.outrule.OutRuleItemVO;
import com.hustcad.plm.rpm.model.vo.outrule.OutRuleParamVO;
import com.hustcad.plm.rpm.model.vo.outrule.OutRuleVO;
import com.hustcad.plm.rpm.model.vo.recent.TyppmOperationRecordVO;
import com.hustcad.plm.rpm.service.job.TyppmJobService;
import com.hustcad.plm.rpm.service.outrule.TyppmOutRuleService;
import com.ty.basic.service.snow.SnowflakeIdComponent;
import com.ty.ppm.api.service.outrule.OutRuleItemService;
import com.ty.ppm.api.service.outrule.OutRuleService;
import com.ty.ppm.model.dto.outrule.OutRuleItemDTO;
import com.ty.ppm.model.entity.outrule.OutRule;
import com.ty.ppm.model.entity.outrule.OutRuleItem;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Aspect
@Component
@Slf4j
@Transactional
public class BreakDownTaskAOP {

    @Resource
    private OutRuleService outRuleService;
    @Resource
    private BaseConvert baseConvert;
    @Resource
    private OutRuleItemService outRuleItemService;
    @Resource
    private SnowflakeIdComponent snowflakeIdComponent;
    @Resource
    private TyppmOutRuleService typpmOutRuleService;
    @Resource
    private RTTaskMapper rtTaskMapper;

    @Around(value = "execution(* com.hustcad.plm.rpm.controller.job.JobController.creatJobListByParentJob(..))")
    public Object creatJobListByParentJob(ProceedingJoinPoint joinPoint) throws Throwable {
        BatchJobDTO batchJobDTO=(BatchJobDTO)joinPoint.getArgs()[0];

        List<TyppmJobDTO> tjd = batchJobDTO.getChildList();

        for (TyppmJobDTO tj : tjd) {
            Map<String,List<String>> map = new HashMap<>();
            map.put("ParentTask", Arrays.asList("true"));
            tj.setIbaAttribute(map);
        }
        TyppmResponseResult<List<TyppmJobDTO>> result = (TyppmResponseResult<List<TyppmJobDTO>>)joinPoint.proceed();

        for (TyppmJobDTO typpmJobDTO : tjd) {

            //继承父任务交付物规则
            Map<String, Object> params = new HashMap<>();
            params.put("containeroid",batchJobDTO.getParentOid());
            params.put("usertype", "USER");
            params.put("jobOid", typpmJobDTO.getParentOID());
            params.put("markfordelete", 0L);
            PageInfo<OutRule> outRulePageInfo = outRuleService.queryOutRuleByPage(params);
            if (CollUtil.isNotEmpty(outRulePageInfo.getList())) {
                List<OutRuleVO> outRules = baseConvert.copyToList(outRulePageInfo.getList(), OutRuleVO.class);
                List<TyppmOutRuleItem> outRuleItems = this.getOutRuleItemList(
                        outRules.stream().map(OutRuleVO::getOid).collect(Collectors.toList()));
                for (OutRuleVO outRule : outRules) {
                    OutRuleParamVO outRuleVO = new OutRuleParamVO();
                    List<TyppmOutRuleItem> outRuleItemList = new ArrayList<>();
                    for (TyppmOutRuleItem outRuleItem : outRuleItems) {
                        if (!outRuleItem.getOutputRuleOid().equals(outRule.getOid()))
                            continue;
                        outRuleItem.setOid(snowflakeIdComponent.getInstance().nextId().toString());
                        outRuleItemList.add(outRuleItem);
                    }
                    outRule.setJobOid(typpmJobDTO.getOid());
                    outRule.setOid(snowflakeIdComponent.getInstance().nextId().toString());
                    outRuleVO.setOutRule(outRule);
                    outRuleVO.setOutRuleItemList(baseConvert.copyToList(outRuleItemList, OutRuleItemVO.class));
                    this.typpmOutRuleService.insertOutRule(outRuleVO);
                }
            }
        }
        if (CollUtil.isNotEmpty(tjd)){
            rtTaskMapper.updateJobState(tjd);
        }
        return result;
    }
    private List<TyppmOutRuleItem> getOutRuleItemList(List<String> oldRuleOidList) {
        OutRuleItemDTO params = new OutRuleItemDTO();
        params.setOldRuleOidList(oldRuleOidList.stream().map(BigInteger::new).collect(Collectors.toList()));
        params.setMarkForDelete(0L);
        List<OutRuleItem> outRuleItems = this.outRuleItemService.listOutRuleItemByRuleOidList(params);
        return this.baseConvert.copyToList(outRuleItems, TyppmOutRuleItem.class);
    }




}
